//
//  SetViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/9.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "SetViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "UserInfo.h"
#import "ChangePhoneViewController.h"
#import "ChangeInfoViewController.h"
#import "ChangePsdViewController.h"
#import "ZYTableViewCell.h"
#import "YXCustomActionSheet.h"
#import "DyShare.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource, YXCustomActionSheetDelegate>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NavView *nav;
@property (nonatomic,retain) NSArray *titleImageNameArr;
@property (nonatomic,retain) NSArray *titleArr;

@end

@implementation SetViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUI];
}
- (void) setUI
{
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"设置"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    //设置cell点击背景色不变
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSArray *titleImageNameArr = [[NSArray alloc]initWithObjects:@"修改手机号码图标",@"修改资料图标",@"修改密码图标",@"分享给好友图标",@"给我们评价图标",@"清除缓存图标", nil];
    NSArray *titleArr = [[NSArray alloc]initWithObjects:@"修改手机号",@"修改个人资料",@"修改密码",@"分享给好友",@"给我们评价",@"清除缓存", nil];
    UIImageView *titleImage = [[UIImageView alloc]init];
    titleImage.frame = CGRectMake(32*kScale, (100-30)*kScale/2, 30*kScale, 30*kScale);
    UILabel *titleLable = [[UILabel alloc]init];
//        titleImage.backgroundColor = [UIColor cyanColor];
    titleLable.frame = CGRectMake(80*kScale, (100-44)*kScale/2, kScreenW, 44*kScale);
    
    titleImage.image = [UIImage imageNamed:titleImageNameArr[indexPath.row]];
    titleLable.text = titleArr[indexPath.row];
        
    [cell addSubview:titleImage];
    [cell addSubview:titleLable];
        
    //右侧小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 5) {
        NSUInteger imagesize = [[SDImageCache sharedImageCache] getSize];
        float floatsize = [[NSNumber numberWithUnsignedInteger:imagesize] floatValue];
        NSString *sizestr = [NSString stringWithFormat:@"%.2fM",floatsize/1024.0/1024.0];
        UILabel *titleLable2 = [[UILabel alloc]init];
        //        titleImage.backgroundColor = [UIColor cyanColor];
        titleLable2.textAlignment = NSTextAlignmentLeft;
        titleLable2.frame = CGRectMake(kScreenW - 100, (100-44)*kScale/2, 100, 44*kScale);
        titleLable2.text = sizestr;
        [cell addSubview:titleLable2];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelector:@selector(cancleSelect) withObject:nil afterDelay:0.1f];
    if (indexPath.row==0)
    {
        ChangePhoneViewController  *vc1 = [[ChangePhoneViewController alloc]init];
        [self.navigationController pushViewController:vc1 animated:YES];
    }
    if (indexPath.row==1)
    {
        ChangeInfoViewController  *vc2 = [[ChangeInfoViewController alloc]init];
        [self.navigationController pushViewController:vc2 animated:YES];
    }
    if (indexPath.row==2)
    {
        ChangePsdViewController  *vc3 = [[ChangePsdViewController alloc]init];
        [self.navigationController pushViewController:vc3 animated:YES];
    }
    if (indexPath.row==3)
    {
        //分享给好友
        [self clickShare];
    }
    if (indexPath.row==4)
    {
        //给我们评价
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1149721016"]];
    }
    if (indexPath.row==5)
    {
        //清除缓存
        [self clearCache];
    }
}

- (void)clickShare {
    YXCustomActionSheet *cusSheet = [[YXCustomActionSheet alloc] init];
    cusSheet.delegate = self;
    NSArray *contentArray = @[@{@"name":@"朋友圈",@"icon":@"sns_pyq"},
                              @{@"name":@"微信",@"icon":@"sns_wx"},
                              @{@"name":@"新浪微博",@"icon":@"sns_wb"},
                              @{@"name":@"QQ ",@"icon":@"sns_qq"},
                              @{@"name":@"QQ空间 ",@"icon":@"sns_qzone"},];
    
    [cusSheet showInView:[UIApplication sharedApplication].keyWindow contentArray:contentArray];
}

- (void)customActionSheetButtonClick:(YXActionSheetButton *)btn {
    NSLog(@"第%li个按钮被点击了",(long)btn.tag);
    NSInteger index = btn.tag;
    UIImage *shareimg = [UIImage imageNamed:@"shareicon"];
    DyShareType type = DyShareTypeWeiXin;
    
    switch (index) {
        case 0:
            type = DyShareTypePengYouQuan;
            break;
            
        case 4:
            type = DyShareTypeQZone;
            break;
        case 2:
            type = DyShareTypeWeiBo;
            break;
        case 1:
            type = DyShareTypeWeiXin;
            break;
        case 3:
            type = DyShareTypeQQ;
            break;
        default:
            break;
    }

    [[DyShare getInstance]shareWithShareType:type Content:@"" title:@"玩艺汇" url:@"https://itunes.apple.com/us/app/han-yi-wang/id1149721016?l=zh&ls=1&mt=8" image:shareimg delegate:nil];
}

- (void)clearCache {
    [self showWithProgress];
    [[SDImageCache sharedImageCache] clearDisk];
}

static float progress = 0.0f;

- (void)showWithProgress {
    progress = 0.0f;
    [SVProgressHUD showProgress:0 status:@"清理中"];
    [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.1f];
}

- (void)increaseProgress {
    progress+=0.1f;
    [SVProgressHUD showProgress:progress status:@"清理中"];
    
    if(progress < 1.0f)
        [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.1f];
    else
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.1f];
}

- (void)dismiss {
    [_mainTableView reloadData];
    [SVProgressHUD dismiss];
}

- (void)cancleSelect
{
    [_mainTableView deselectRowAtIndexPath:[_mainTableView indexPathForSelectedRow] animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
//修改状态栏字体
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
